package com.hackathon.sgstern.joinevent;

import java.text.SimpleDateFormat;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.hackathon.sgstern.R;
import com.hackathon.sgstern.SurroundingArea;
import com.hackathon.sgstern.adapter.PersonAdapter;
import com.hackathon.sgstern.dataprovider.DataProvider;
import com.hackathon.sgstern.dataprovider.LogoProvider;
import com.hackathon.sgstern.model.Person;
import com.hackathon.sgstern.model.SportEvent;
import com.hackathon.sgstern.utilities.AddressUtilities;

public class JoinEvent extends Activity {

	private static final SimpleDateFormat dateFormat = new SimpleDateFormat(
			"dd.MM.yyyy hh:mm", Locale.GERMANY);

	public static final String CENTER_EVENT_INTENT = "centerEvent";

	ImageView typeLogo;
	TextView title;
	TextView address;
	TextView date;
	TextView participantNumber;
	ListView participantList;

	Button delete;

	MenuItem joinMenuItem;
	MenuItem cancelMenuItem;

	PersonAdapter personAdapter;

	SportEvent event;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_join_event);
		typeLogo = (ImageView) findViewById(R.id.joinTypeIcon);
		title = (TextView) findViewById(R.id.joinTitle);
		address = (TextView) findViewById(R.id.joinAddress);
		date = (TextView) findViewById(R.id.joinDate);
		participantList = (ListView) findViewById(R.id.joinParticipantlist);
		participantNumber = (TextView) findViewById(R.id.joinevent_participant_number);

		// Show the Up button in the action bar.
		setupActionBar();

		Bundle extras = getIntent().getExtras();
		if (extras.containsKey(SurroundingArea.JOIN_EVENT_KEY)) {
			event = (SportEvent) extras.get(SurroundingArea.JOIN_EVENT_KEY);
			typeLogo.setImageBitmap(LogoProvider
					.retrieveBitmap(this, event.art));
			title.setText(event.title);
			address.setText(AddressUtilities.getAddressOfPoint(event.getOrt(),
					this));
			date.setText(dateFormat.format(event.time));
			participantNumber.setText(event.participants.size() + "/"
					+ event.maxTeilnehmer);
			personAdapter = new PersonAdapter(this);
			personAdapter.persons = event.participants;
			participantList.setAdapter(personAdapter);
		}

	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.join_event, menu);

		MenuItem item = menu.findItem(R.id.action_join_delete);
		if (event.creator.equals(DataProvider.getInstance().getCurrentUser())) {
			item.setVisible(true);
		} else {
			item.setVisible(false);
		}

		joinMenuItem = menu.findItem(R.id.action_join_event);
		cancelMenuItem = menu.findItem(R.id.action_cancel_join);
		updateMenuState();
		return true;
	}

	void updateMenuState() {
		boolean isEventFull = event.participants.size() == event.maxTeilnehmer;
		boolean hasUserjoinedEvent = event.participants.contains(DataProvider
				.getInstance().getCurrentUser());
		if (isEventFull || hasUserjoinedEvent) {
			joinMenuItem.setVisible(false);
		} else {
			joinMenuItem.setVisible(true);
		}
		if (hasUserjoinedEvent) {
			cancelMenuItem.setVisible(true);
		} else {
			cancelMenuItem.setVisible(false);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_join_maps:
			onShowInMaps();
			return true;
		case R.id.action_join_event:
			registerToEvent();
			updateMenuState();
			updateParticipants();
			return true;
		case R.id.action_cancel_join:
			cancelEvent();
			updateMenuState();
			updateParticipants();
			return true;
		case R.id.action_join_delete:
			onDelete();
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void updateParticipants() {
		personAdapter.persons = event.participants;
		personAdapter.notifyDataSetChanged();
		participantNumber.setText(event.participants.size() + "/"
				+ event.maxTeilnehmer);
	}

	private void registerToEvent() {
		DataProvider provider = DataProvider.getInstance();
		Person user = provider.getCurrentUser();
		event = provider.addUserToEvent(event, user);
	}

	private void cancelEvent() {
		DataProvider provider = DataProvider.getInstance();
		Person user = provider.getCurrentUser();
		event = provider.removeUserFromEvent(event, user);
	}

	void onShowInMaps() {
		Intent showOnMap = new Intent(this, SurroundingArea.class);
		showOnMap.putExtra(CENTER_EVENT_INTENT, event);
		startActivity(showOnMap);
	}

	void onDelete() {
		DataProvider.getInstance().getEvents().remove(event);
	}

}
